package com.okhttp.retrofit;

import com.okhttp.utils.RetrofitLog;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import retrofit2.Call;
import retrofit2.Response;

/**
 * @author GYL
 * @dateTime 2020/4/21-15:50
 * @description 请求回调Response
 */
public abstract class BaseResponseHandler<E, T extends BaseResponse> implements CallbackStub<E, T> {
  
  private static final String TAG = "BaseResponseHandler";
  private int mWhat;
  private final ReferenceQueue<IHandlerContext> mRfq=new ReferenceQueue<>();
  private IHandlerContext mHContext;
  
  public BaseResponseHandler(){
    this(null);
  }
  
  public BaseResponseHandler(IHandlerContext handlerContext){
    if(handlerContext!=null){
      final WeakReference<? extends IHandlerContext> mWrfContext=new WeakReference<>(handlerContext,mRfq);
      mHContext=mWrfContext.get();
    }
  }
  
  protected String getString(int strId,String def){
//    if(mHContext!=null && mHContext.getCurrentContext()!=null){
//      return mHContext.getCurrentContext().getString(strId);
//    }
    return def;
  }
  
  protected void showProgress(int what){
    if(mHContext!=null){
      mHContext.showProgress("");
    }
  }
  
  protected void dismissProgress(int what){
    if(mHContext!=null){
      mHContext.dismissProgress();
    }
  }
  
  @Override
  public void onBeforeExecute(int what) {
    this.mWhat = what;
    this.showProgress(mWhat);
  }
  
  @Override
  public E convertBody(T t) {
    return (E) t.data();
  }
  
  @Override
  public void onResponse(Call<T> call, Response<T> response) {
    try {
      dismissProgress(mWhat);
      RetrofitLog.d(TAG, "response code:"+response.code()+" message:"+response.message());
      if (response.isSuccessful()) {
        T result = response.body();
        if(result==null){
          RetrofitLog.e(TAG, "result:null");
          if(DISK_CACHE_DATA.equals(response.message())
             || NET_DATA.equals(response.message())){
            onHandler(mWhat, String.valueOf(ERROR_NO_DATA), getString(0,"暂无数据"), null);
          }else{
            onHandler(mWhat, ERROR_CODE, getString(0,"暂无数据"), null);
          }
        }else {
          onHandler(mWhat, String.valueOf(result.code()), result.msg(), convertBody(result));
        }
      } else {
        if(ERROR_NET_INVALID==response.code()){
          onHandler(mWhat, String.valueOf(response.code()), getString(0,"网络未联接,请检查网络"), null);
        }else if(ERROR_NET_TIMEOUT==response.code()){
          onHandler(mWhat, String.valueOf(response.code()), getString(0,"请求超时"), null);
        }else{
          onHandler(mWhat, String.valueOf(response.code()), getString(0,"请求失败"), null);
        }
      }
    } catch (Exception e) {
      RetrofitLog.e(TAG, e.getMessage(), e);
      onHandler(mWhat, ERROR_CODE, getString(0,"请求失败"), null);
    } finally {
      call.cancel();
    }
  }
  
  @Override
  public void onFailure(Call<T> call, Throwable t) {
    dismissProgress(mWhat);
    RetrofitLog.e(TAG, t.getMessage(), t);
    call.cancel();
    onHandler(mWhat, ERROR_CODE, getString(0,"请求失败"), null);
  }
  
}
